package com.fzrskj.cloud.free.api;

import com.fzrskj.cloud.free.core.tool.CEncryptTool;
import com.fzrskj.cloud.free.core.tool.CHttpTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.dic.utils.DicParamUtils;
import com.fzrskj.cloud.free.user.utils.UserLogUtils;
import net.sf.json.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class DdkjSmsAPI {
	//点集科技短信接口
	public String dianjiSms(String param) {

		CHttpTool http = new CHttpTool();
		JSONObject obj_result = new JSONObject();
		JSONObject json = JSONObject.fromObject(param);
		int userId= CStrTool.strToIntDef(json.getString("userId"), 0);
		String phonenumber = json.getString("Tel");
		String msgContent = json.getString("Content");
		String sendsmsurl = DicParamUtils.getParamValue("hlSmsUrl");
		String account = DicParamUtils.getParamValue("hlUsername");
		String password = DicParamUtils.getParamValue("hlToken");

		//基本参数判断
		if ("".equals(sendsmsurl) ||"".equals(account) || "".equals(password) ) {
			obj_result.put("code", "-1");
			obj_result.put("message", "接口参数不全");
			return obj_result.toString();
		}

		if ("".equals(phonenumber) ||"".equals(msgContent) ) {
			obj_result.put("code", "-1");
			obj_result.put("message", "手机号码或短信内容不能为空");
			return obj_result.toString();
		}

		long timestamps = System.currentTimeMillis();//时间戳
		List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("account", account));
		params.add(new BasicNameValuePair("password", CEncryptTool.md5Encode(password+phonenumber+timestamps)));
		params.add(new BasicNameValuePair("mobile",phonenumber));
		params.add(new BasicNameValuePair("content",msgContent));
		params.add(new BasicNameValuePair("timestamps",timestamps+""));

		String result=http.httpPost(sendsmsurl, params, "UTF-8");

		//写入日志
		UserLogUtils.addLog(this, userId, "dianjiSms", "发送报文=【"+sendsmsurl+"?"+param.toString()+"】,接收报文=【"+result+"】");
		

		obj_result.put("code", "0");
		obj_result.put("result", result);
		obj_result.put("account", account);
		http=null;
		return obj_result.toString();	
	}
}
